﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Drawing.Imaging;

namespace BBS.Bll.Sys
{
    /// <summary>
    /// 验证码
    /// </summary>
    public static class VerificationCode
    {
        public static void DrawCode(HttpContext httpContext)
        {
            var code = GetCode();
            httpContext.Session.Add(SessionField.SessionVerificationCode, code);
            Image m = Image.FromFile(httpContext.Server.MapPath("~/Image/vc120x30.gif"));
            Graphics g;
            if (IsPixelFormatIndexed(m.PixelFormat))
            {
                Bitmap bmp = new Bitmap(m.Width, m.Height, PixelFormat.Format32bppArgb);
                g = Graphics.FromImage(bmp);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(m, 0, 0);
                for (int i = 0; i < code.Length; i++)
                {
                    g.DrawString(code[i].ToString(), new Font("宋体", 14), Brushes.Blue, i*20+20.0f, 5.0f);
                }
                using (var ms = new System.IO.MemoryStream())
                {
                    bmp.Save(ms, ImageFormat.Gif);
                    httpContext.Response.BinaryWrite(ms.ToArray());
                }
            }
            else //否则直接操作
            {
                g = Graphics.FromImage(m);
                for (int i = 0; i < code.Length; i++)
                {
                    g.DrawString(code[i].ToString(), new Font("宋体", 14), Brushes.Blue, i * 20 + 20.0f, 5.0f);
                }
                using (var ms = new System.IO.MemoryStream())
                {
                    m.Save(ms, ImageFormat.Gif);
                    httpContext.Response.BinaryWrite(ms.ToArray());
                }
            }
            m.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 验证码验证不区分大小写
        /// </summary>
        /// <param name="code"></param>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static bool CheckCode(string code,HttpContext httpContext)
        {
            var scode = httpContext.Session[SessionField.SessionVerificationCode];
            if (null == scode || null == code)
            {
                return false;
            }
            return code.ToLower().Trim() == scode.ToString().ToLower().Trim();
        }

        private static string GetCode()
        {
            const string code = "qwertyuipkjhgfdsazxcvbnmQWERTYUOPLKJHGFDSAZXCVBNM23456789";
            var random = new Random();
            const int codeLength = 4;
            var sb = new StringBuilder();
            for (int i = 0; i < codeLength; i++)
            {
                var a = code[random.Next(code.Length)];
                sb.Append(a);
            }
            return sb.ToString();
        }

        //会产生graphics异常的PixelFormat
        private static PixelFormat[] indexedPixelFormats = {
                                                               PixelFormat.Undefined, PixelFormat.DontCare,
                                                               PixelFormat.Format16bppArgb1555,
                                                               PixelFormat.Format1bppIndexed,
                                                               PixelFormat.Format4bppIndexed,
                                                               PixelFormat.Format8bppIndexed
                                                           };

        private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
        {
            foreach (PixelFormat pf in indexedPixelFormats)
            {
                if (pf.Equals(imgPixelFormat)) return true;
            }

            return false;
        }


    }
}
